<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div>
        <input type="file" name="file" onchange="uploadFile(this)">
    </div>
    <div>
        <pre id="ret-content"></pre>
    </div>
    <script>
        function uploadFile(t){
            if(t.files.length <= 0) return;

            let fm = new FormData();
            
            fm.append('file',t.files[0]);
                    // data
            // fetch('/data',{
            fetch('/upload',{
                method:'POST',
                body:fm
            })
            .then(res =>{
                if(res.ok){
                    return res.text();
                }
                throw new Error(`${res.status}${res.statusText}`)
            })
            .then(text => {
                // document.getElementById('ret-content').innerHTML = text   //显示文件名称
                // 显示图片
                let d = document.getElementById('ret-content');
                // 前端src是给后端发起get请求
                d.innerHTML = `<img src="/image/${text}" style="width:80%;hright:auto;">`;

            })
            .catch(err => {
                cosnole.log(err.message);
            })
        }
    </script>
</body>
</html>  